<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermission(1, '0')">添加权限</el-button>
        </div>
        <el-table :data="permissionList" row-key="id">
          <el-table-column prop="name" label="菜单名称" sortable width="180" />
          <el-table-column prop="code" label="权限标识" sortable width="180" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" size="small" type="text" @click="addPermission(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template></el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="showTitle" :close-on-click-modal="false" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form ref="addForm" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionListApi, addPermissionApi, delPermissionApi, getPermissionDetailApi, updatePermissionApi } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 编辑的时候更改title名称
    showTitle() {
      return this.formData.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 打开页面渲染列表
    async getPermissionList() {
      const { data } = await getPermissionListApi()
      // console.log(data)
      // 树状解构只显示父级
      this.permissionList = transListToTreeData(data, '0')
      // console.log(this.permissionList)
    },
    // 点击删除询问是否删除，获取id提示删除成功重置表单
    async delPermission(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        await delPermissionApi(id)
        // 添加提示
        this.$message.success('删除角色成功')
        this.getPermissionList()
      }).catch(() => { })
    },
    // 点击添加出弹框
    // type是添加父级菜单
    // pid是添加子菜单
    addPermission(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 点击添加的确定上传数据
    async clickSubmit() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        // 修改
        if (this.formData.id) {
          await updatePermissionApi(this.formData)
          this.$message.success('恭喜！修改成功')
        } else {
          // 添加
          await addPermissionApi(this.formData)
          this.$message.success('恭喜！添加成功')
        }
        this.getPermissionList()
        this.showDialog = false
      })
    },
    // 点击确定关闭弹窗重置表单数据
    closeDialog() {
      this.showDialog = false
      this.$refs.addForm.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 点击查看数据回填
    async edit(id) {
      this.showDialog = true
      const { data } = await getPermissionDetailApi(id)
      this.formData = data
    }
  }
}

</script>

<style>

</style>
